package com.zh.rent.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.Invitation;
import com.zh.rent.pojo.StandbyAccount;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.InvitationService;
import com.zh.rent.util.ExpireTime;
import com.zh.rent.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bacInv")
@Api(tags="邀请码后台管理")
public class BacInvController {
    @Autowired
    InvitationService InvitationServiceImpl;
    @Autowired
    AccountService AccountServiceImpl;

    @PostMapping("/bacInvSelAll")
    @ResponseBody
    @ApiOperation(value = "邀请码显示")
    public JsonResult bacInvSelAll(@RequestParam(value="pageSize",defaultValue="5")Integer pageSize,
                                      @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
                                      String name) {
        Map<String,Object> map = new HashMap<>();
        name = "%"+name+"%";
        map.put("name",name);
        PageInfo<Map<String,Object>> mapPageInfo=null;
        mapPageInfo = InvitationServiceImpl.selInvAll(pageNum, pageSize, map);
        for (Map<String, Object> stringObjectMap : mapPageInfo.getList()) {
            stringObjectMap.put("CreationTime",stringObjectMap.get("CreationTime").toString());
            String uTime=null;
            uTime=(String) stringObjectMap.get("useTime");
            if(!uTime.equalsIgnoreCase("")) {
                //计算到期时间
                String s = ExpireTime.plusDay((int) stringObjectMap.get("residue"), (String) stringObjectMap.get("useTime"));
                stringObjectMap.put("expireTime",s);
            }else {
                stringObjectMap.put("expireTime","");
            }
        }

        return JsonResult.OK(mapPageInfo);
    }

    @PostMapping("/bacInvupdate")
    @ResponseBody
    @ApiOperation(value = "邀请码修改")
    public JsonResult bacInvupdate(Integer Invitationid,
                                      Integer InvitationDays,
                                      Double BuyMoney,
                                      String Voucher,
                                      Integer logNum,
                                      Integer residue,
                                      Integer Accountid,
                                      String AccountType) {
        try{
        Invitation invitation = new Invitation();
        invitation.setInvitationid(Invitationid);//id
        //invitation.setInvitationCode(InvitationCode);//邀请码
        invitation.setInvitationDays(InvitationDays);//天数
        invitation.setBuyMoney(BuyMoney);//金额
        invitation.setVoucher(Voucher);//凭证
       // invitation.setState(state);//状态
        invitation.setLogNum(logNum);//登录次数
        invitation.setResidue(residue);//到期天数
        invitation.setAccountid(Accountid);//账号id
        invitation.setAccountType(AccountType);//类型
        //修改
        int i = InvitationServiceImpl.updateInv(invitation);
        if(i==0){
            return JsonResult.ERROR("修改失败");
        }
        return JsonResult.OK("修改成功");
    }catch (Exception e){
        return JsonResult.ERROR("发现错误："+e.getMessage());
    }
    }

    @PostMapping("/InvBacdeleteAll")
    @ResponseBody
    @ApiOperation(value = "邀请码根据id批量删除")
    public JsonResult InvBacdeleteAll(String Invitationid) {
        try {
            String buy= Invitationid.substring(1,Invitationid.length()-1);
            List<Integer> list = new ArrayList<Integer>();
            String[] str = buy.split(",");
            for (int i = 0; i < str.length; i++) {
                int i1 = Integer.parseInt(str[i]);
                list.add(i1);
            }
            Integer i = InvitationServiceImpl.delInvBccAll(list);
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }

    @PostMapping("/InvBacdelete")
    @ResponseBody
    @ApiOperation(value = "邀请码根据id删除")
    public JsonResult InvBacdelete(Integer Invitationid) {
        try {
            Integer i = InvitationServiceImpl.delInvBccid(Invitationid);
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }


    @GetMapping("/upInvgive")
    @ResponseBody
    @ApiOperation(value = "邀请码批量出售")
    public JsonResult upInvgive(String Invitationid,String Voucher) {
        try {
            if(Invitationid.equalsIgnoreCase("[]")){
                return JsonResult.OK();
            }
            String buy = Invitationid.substring(1, Invitationid.length() - 1);
            List<Integer> list = new ArrayList<Integer>();
            String[] str = buy.split(",");
            for (int i = 0; i < str.length; i++) {
                int i1 = Integer.parseInt(str[i]);
                list.add(i1);
            }
            //对账号的邀请码数量减一，出售计数加一
            for (Integer integer : list) {
                Invitation invitation = InvitationServiceImpl.upInvgive(integer);
                AccountServiceImpl.updateAccDis(invitation.getAccountid());
                Map<String, Object> map2 = new HashMap<>();
                map2.put("Invitationid", integer);//邀请码id
                map2.put("state", 2);//状态
                map2.put("Voucher", Voucher);//凭证*//*
                //修改邀请码信息
                int  i = InvitationServiceImpl.updateInvDis(map2);
            }
            return JsonResult.OK("邀请码批量出售成功");
        }catch (Exception e){
            return JsonResult.ERROR("出错："+e.getMessage());
        }
    }
}
